package com.tencent.wxcloudrun.controller;

import com.github.pagehelper.PageInfo;
import com.tencent.wxcloudrun.common.ThreadLocalUtils;
import com.tencent.wxcloudrun.model.AdminUser;
import com.tencent.wxcloudrun.response.R;
import com.tencent.wxcloudrun.service.AdminUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * ClassName: AdminUserController
 * Package: com.tencent.wxcloudrun.controller
 * Description:
 *
 * @Date: 2023/1/22 8:34 AM
 * @Author: wht
 */
@Slf4j
@RestController
//@RestController = @Controller + @ResponseBody
@RequestMapping("/admin")
public class AdminUserController {
    @Autowired
    AdminUserService adminUserService;

    @PostMapping("/login")
    public <T> R<T> login(@RequestBody AdminUser adminUser) {
//        AdminUser adminUser1 = new AdminUser();
//        adminUser1.setMobile("15503593230");
//        adminUser1.setStatus(1);
//        adminUserService.addOneAdminUser(adminUser1);

        String token = adminUserService.login(adminUser);
        return R.success("登录成功！", token);
    }

    @GetMapping("/getCosTempToken")
    public <T> R<T> getCosTempToken() {
        HashMap res = adminUserService.getCosTempToken();
        return R.successData(res);
    }

    @GetMapping("/getOneAdminUserByToken")
    public <T> R<T> getOneAdminUserByToken() {
        AdminUser adminUser = ThreadLocalUtils.getUser();
        return R.successData(adminUser);
    }

    @PutMapping("/addOneAdminUser")
    public <T> R<T> addOneAdminUser(@RequestBody AdminUser adminUser) {
        adminUserService.addOneAdminUser(adminUser);
        return R.successMsg("添加成功！");
    }

    @PatchMapping("/resetAdminUserPassword")
    public <T> R<T> resetAdminUserPassword(@RequestBody AdminUser adminUser) {
        adminUserService.resetAdminUserPassword(adminUser);
        return R.successMsg("修改成功！");
    }

    @PatchMapping("/updateOneAdminUserById")
    public <T> R<T> updateOneAdminUserById(@RequestBody AdminUser adminUser) {
        adminUserService.updateOneAdminUserById(adminUser);
        return R.successMsg("修改成功！");
    }

    @PostMapping("/getAdminUserList")
    public <T> R<T> getAdminUserList(@RequestBody AdminUser adminUser) {
        PageInfo adminUsers = adminUserService.getAdminUserList(adminUser);
        return R.successData(adminUsers);
    }

    @GetMapping("/getOneAdminUserById/{id}")
    public <T> R<T> getOneAdminUserById(@PathVariable("id") Integer id) {
        AdminUser adminUser = adminUserService.getOneAdminUserById(id);
        return R.successData(adminUser);
    }

}
